//
//  login.m
//  Project2
//
//  Created by hundredsandc on 2017/11/7.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "login.h"
#import "registVC.h"
@interface login ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *usernameTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation login

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    _loginBtn.layer.cornerRadius = 20.0f;
    _loginBtn.layer.masksToBounds = YES;
    [_passwordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_usernameTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resignTF)];
    [self.view addGestureRecognizer:tap];
    
}
-(void)resignTF{
    [_usernameTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.text=@"";
    if (textField==_usernameTF) {
        if (![textField.text isEqualToString:@"请输入账号"]) {
            
        }
    }
    if (textField==_passwordTF) {
        textField.secureTextEntry = YES;
        
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField==_usernameTF) {
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"请输入账号";
        }
    }
    if (textField==_passwordTF) {
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"请输入密码";
            textField.secureTextEntry = NO;
        }
    }
}
-(void)textFieldDidChange:(UITextField *)theTextField{
    if (theTextField==_usernameTF) {
        if (![theTextField.text isEqualToString:@"请输入账号"]) {
        
        }
    }
    if (theTextField==_passwordTF) {
        
    }
}
- (IBAction)login:(id)sender {
    if (_usernameTF.text == nil || [_usernameTF.text removeWhitespaceAndNewlinewithboolNewLine:YES].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入用户名"];
        return;
    }
    if (_passwordTF.text.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"密码至少6位"];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = _usernameTF.text;
    params[@"password"] = _passwordTF.text;
    
    [HTTPManagers postForumUserWithURL:@"login" params:params success:^(id json) {
        if ([json[@"code"]integerValue]==0) {
            [UserDefault setObject:_usernameTF.text forKey:CurrentUser];
            [UserDefault synchronize];
            [SVProgressHUD showSuccessWithStatus:@"登陆成功" maskType:SVProgressHUDMaskTypeBlack];
            [self performSelector:@selector(pop) withObject:nil afterDelay:1];
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:@"登陆失败" maskType:SVProgressHUDMaskTypeBlack];
        }
    } failure:^(NSError *error) {
        
    }];

}
-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)register:(id)sender {
    registVC *vc = [[registVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
